package com.lx.pc.service.broadcast.impl;

import java.math.BigInteger;
import java.util.List;

import org.springframework.stereotype.Service;

import com.lx.pc.domain.entity.broadcast.BroadcastEntity;
import com.lx.pc.domain.vo.broadcast.BroadcastVo;
import com.lx.pc.mybatis.repository.broadcast.BroadcastRepository;
import com.lx.pc.service.AbstractService;
import com.lx.pc.service.broadcast.IBroadcastService;
import com.lx.pc.service.exception.DataFindException;
import com.lx.pc.service.exception.DataSaveException;
import com.lx.pc.utils.OmsPage;

@Service("broadcastService")
public class BroadcastServiceImpl extends AbstractService<BroadcastEntity, BroadcastVo, BigInteger, BroadcastRepository> implements IBroadcastService{

	@Override
	public Boolean saveBroadcast(BroadcastVo broadcast) {
		Boolean flag = false;
		if (broadcast == null) {
			return flag;
		}
		broadcast.setStatus((short) 0);
		try {
			this.save(broadcast);
			flag = true;
		} catch (Exception e) {
		}
		return flag;
	}

	@Override
	public String updateBroadcast(BroadcastVo broadcast) {
		if (broadcast == null || broadcast.getId() == null) {
			return null;
		}
		BroadcastVo condition = new BroadcastVo();
		condition.setId(broadcast.getId());
		try {
			this.update(condition, broadcast);
			return broadcast.getId().toString();
		} catch (Exception e) {
		}
		return null;
	}

	@Override
	public int findByBroadcastCount(BroadcastVo broadcastVo) {
		return repository.findByBroadcastCount(broadcastVo);
	}

	@Override
	public OmsPage<BroadcastVo> findByBroadcast(BroadcastVo broadcastVo, Integer page, Integer size) {
		OmsPage<BroadcastVo> pages = new OmsPage<BroadcastVo>();
		pages.setTotal(repository.findByBroadcastCount(broadcastVo));
		List<BroadcastEntity> list = repository.findByBroadcast(broadcastVo,page = (page - 1) * size,size);
		pages.setResult(etoV(list));
		return pages;
	}

	@Override
	public BroadcastVo findByBroadcastId(BigInteger id) {

		return etoV(repository.findByBroadcastId(id));
	}

	@Override
	public String upload(byte[] decode, String fileName) {

		if (decode == null) {
			throw new DataFindException("上传图片不能为空");
		}
		String logo = null;
		try {
			logo = "";//aliyunRpcApi.uploadFile(SourceEnum.GONGREN, "dds/img/oddjob/logo", fileName, decode);
		} catch (Exception e) {
			throw new DataSaveException("上传图片，保存阿里云出错", e);
		}
		return logo;
	}

}
